﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using MShop.Core.Extensions ;
using MShop.Core.DataAccess;
using MShop.Core.Service.Membership;

namespace MShop.Web.Components.ModelBinders
{
    public class ProductBinder:System.Web.Mvc.DefaultModelBinder
    {

        private readonly IItemCatDao _ItemCatDao;
        private readonly IProductDao _ProductDao;
        private Product _Product;
        private ItemCat _ItemCat;
        public ProductBinder(IItemCatDao itemCatDao, IProductDao productDao)
        {
            _ItemCatDao = itemCatDao;
            _ProductDao = productDao;
        }

        protected override object CreateModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, Type modelType)
        {
            _Product = (Product)base.CreateModel(controllerContext, bindingContext, modelType);

            HttpRequest Request = HttpContext.Current.Request;
          
            int cid = 0;
            if (Request.Params["cid"] != null)
            {
                cid = int.Parse(Request["cid"]);
            }
            else
            {
                cid = int.Parse(_ItemCatDao.BuildSelectList()[0].Value);
            }
            _ItemCat = _ItemCatDao.GetById(cid);
            _Product.ItemCat = _ItemCat;

            return _Product;
        }

       

      
        public override object BindModel(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
        {
            HttpRequestBase Request = controllerContext.HttpContext.Request;
             _Product  = (Product)base.BindModel(controllerContext, bindingContext);

           
             var itemCat = _Product.ItemCat;
             for (int i = 0; i < itemCat.ItemCatProp.Count; i++)
             {
                 ItemCatProp prop = _Product.ItemCat.ItemCatProp.ToList()[i];
                 string propValue = Request.Form[prop.ItemProp.Field];

                 if (prop.ItemProp.IsKeyProp)
                 {
                     if (string.IsNullOrEmpty(propValue))
                     {
                          bindingContext.ModelState.AddModelError(prop.ItemProp.Field, string.Format("{0}不能为空", prop.ItemProp.Name));
                     }
                     else
                     {
                         if (prop.ItemProp.IsInputProp)
                         {
                             ProductProp productProp = new ProductProp()
                             {
                                 ProductId = _Product.Id,
                                 ItemCatPropId = prop.Id,
                                 ItemPropId = prop.ItemProp.Id,
                                 PropName = prop.ItemProp.Name,
                                 PropValue = RemoveChar(propValue)
                             };
                             _Product.ProductProp.Add(productProp);
                         }
                         else
                         {
                             if (prop.ItemProp.Multi)
                             {
                                 string[] values = propValue.Split(',');
                                 foreach (string str in values)
                                 {
                                     ProductProp productProp = new ProductProp()
                                     {
                                         ProductId = _Product.Id,
                                         ItemPropId = prop.ItemProp.Id,
                                         ItemCatPropId = prop.Id,
                                         PropName = prop.ItemProp.Name,
                                         PropValue = RemoveChar(str)
                                     };
                                     _Product.ProductProp.Add(productProp);
                                 }
                             }
                             
                             else
                             {
                                 ProductProp productProp = new ProductProp()
                                 {
                                     ItemCatPropId = prop.Id,
                                     ItemPropId = prop.ItemProp.Id,
                                     PropName = prop.ItemProp.Name,
                                     PropValue = propValue 
                                 };
                                 _Product.ProductProp.Add(productProp);
                             }
                             
                         }
                     }
                 }
                 else if (prop.ItemProp.IsSaleProp)
                 {
                     if (string.IsNullOrEmpty(propValue))
                     {
                         bindingContext.ModelState.AddModelError(prop.ItemProp.Field, string.Format("{0}不能为空", prop.ItemProp.Name));
                     }
                     else
                     {

                         if (prop.ItemProp.IsInputProp)
                         {
                             ProductSkuProp productProp = new ProductSkuProp()
                             {
                                 ItemCatPropId = prop.Id,
                                 ItemPropId = prop.ItemProp.Id,
                                 PropName = prop.ItemProp.Name,
                                 PropValue = propValue
                             };
                             _Product.ProductSkuProp.Add(productProp);
                         }
                         else
                         {
                             if (prop.ItemProp.Multi)
                             {
                                 string[] values = propValue.Split(',');
                                 foreach (string str in values)
                                 {
                                     ProductSkuProp productProp = new ProductSkuProp()
                                     {
                                         ItemCatPropId = prop.Id,
                                         ItemPropId = prop.ItemProp.Id,
                                         PropName = prop.ItemProp.Name,
                                         PropValue = str
                                     };
                                     _Product.ProductSkuProp.Add(productProp);
                                 }
                             }
                             else
                             {
                                 ProductSkuProp productProp = new ProductSkuProp()
                                 {
                                     ItemCatPropId = prop.Id,
                                     ItemPropId = prop.ItemProp.Id,
                                     PropName = prop.ItemProp.Name,
                                     PropValue = propValue 
                                 };
                                 _Product.ProductSkuProp.Add(productProp);
                             }
                         }


                     }
                 }
                 else if (prop.ItemProp.IsItemProp)
                 {
                     if (!string.IsNullOrEmpty(propValue))
                     {
                         if (prop.ItemProp.IsInputProp)
                         {
                             ProductProp productProp = new ProductProp()
                             {
                                 ProductId = _Product.Id,
                                 ItemPropId = prop.ItemProp.Id,
                                 ItemCatPropId = prop.Id,
                                 PropName = prop.ItemProp.Name,
                                 PropValue = propValue
                             };
                             _Product.ProductProp.Add(productProp);
                         }
                         else
                         {


                             if (prop.ItemProp.Multi)
                             {
                                 if (propValue.Contains(","))
                                 {

                                     string[] values = propValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                     foreach (string str in values)
                                     {
                                         ProductProp productProp = new ProductProp()
                                         {
                                             ProductId = _Product.Id,
                                             ItemPropId = prop.ItemProp.Id,
                                             ItemCatPropId = prop.Id,
                                             PropName = prop.ItemProp.Name,
                                             PropValue = str
                                         };
                                         _Product.ProductProp.Add(productProp);
                                     }

                                 }


                             }
                             else
                             {
                                 ProductProp productProp = new ProductProp()
                                 {
                                     ItemCatPropId = prop.Id,
                                     ItemPropId = prop.ItemProp.Id,
                                     PropName = prop.ItemProp.Name,
                                     PropValue = propValue
                                 };
                                 _Product.ProductProp.Add(productProp);
                             }
                             
                             
                         }
                     }
                 }
             }     

             return _Product;



        }


        private string RemoveChar(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                str = str.Replace(";", "").Replace(",", "").Replace("{", "").Replace("}", "").Replace("|", "");
            }
            return str;
        }
    }
}